package v1

import (
	"github.com/gin-gonic/gin"
	"github.com/scggdani/Blog/internal/service"
	"github.com/scggdani/Blog/pkg/app"
	"github.com/scggdani/Blog/pkg/errcode"
)

type Tag struct { }

func NewTag() Tag {
	return Tag{}
}

func (t Tag) Get(ctx *gin.Context) { }

func (t Tag) List(ctx *gin.Context) {
	param := service.TagListRequest{}
	response := app.NewResponse(ctx)
	valid, errs := app.BindAndValid(ctx, &param)
	if !valid {
		errResp := errcode.InvalidParams.WithDetails(errs.Errors()...)
		response.ToErrorResponse(errResp)
		return
	}
	svc := service.New(ctx.Request.Context())
	pager := app.Pager{Page: app.GetPage(ctx), PageSize: app.GetPageSize(ctx)}
	totalRows, err := svc.CountTag(&service.CountTagRequest{Name: param.Name, State: param.State})
	if err != nil {
		response.ToErrorResponse(errcode.ErrorCountTagFail)
		return
	}
	tags, err := svc.GetTagList(&param, &pager)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorGetTagListFail)
		return
	}
	response.ToResponseList(tags, totalRows)
	return
}

func (t Tag) Create(ctx *gin.Context) {
	param := service.CreateTagRequest{}
	response := app.NewResponse(ctx)
	valid, errs := app.BindAndValid(ctx, &param)
	if !valid {
		errResp := errcode.InvalidParams.WithDetails(errs.Errors()...)
		response.ToErrorResponse(errResp)
		return
	}
	svc := service.New(ctx.Request.Context())
	err := svc.CreateTag(&param)
	if err != nil {
		response.ToErrorResponse(errcode.ErrorCreateTagFail)
		return
	}
	response.ToResponse(gin.H{"code":200, "message":"创建标签成功"})
	return
}

func (t Tag) Update(ctx *gin.Context) { }

func (t Tag) Delete(ctx *gin.Context) { }
